﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Wyn.PMS.Common.NightAudit
{
    public class FormulaProvider
    {
        public const string AvgRoomPrice = "AvgRoomPrice";

        public const string LettingRate = "LettingRate";

        private static volatile FormulaProvider _instance;

        private static readonly object _locker = new object();

        private static IList<Formula> _formulas;

        private static int _pressure;

        private FormulaProvider(){}

        public static FormulaProvider Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_locker)
                    {
                        if (_instance == null)
                        {
                            var provider = new FormulaProvider();

                            _formulas = provider.LoadFormulas();

                            _instance = provider;

                        }
                    }
                }

                return _instance;
            }
        }

        public IList<Formula> Formulas 
        { 
            get
            {
                if (_formulas == null || _formulas.Count <= 0)
                {
                    lock (_locker)
                    {
                        if (_formulas == null || _formulas.Count <= 0)
                        {
                            _formulas = LoadFormulas();
                        }
                    }
                }
                return _formulas;
            }
        }

        public Formula GetFormula(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return null;
            }

            return Formulas.FirstOrDefault(k => k.Name == name);
        }

        private IList<Formula> LoadFormulas()
        {
            XmlDocument doc = new XmlDocument();

            string path = GetRootPath() + @"formula.xml";

            doc.Load(path);

            XmlNodeList nodes = doc.SelectNodes("/formula/exp");

            _formulas = new List<Formula>();

            foreach (XmlNode node in nodes)
            {
                Formula exp = new Formula();

                exp.Name = node.Attributes["name"].Value;

                exp.CheckInType_NS = double.Parse(node.ChildNodes[0].Attributes["value"].Value);

                exp.CheckInType_DR = double.Parse(node.ChildNodes[1].Attributes["value"].Value);

                exp.CheckInType_HR = double.Parse(node.ChildNodes[2].Attributes["value"].Value);

                exp.CheckInType_FR = double.Parse(node.ChildNodes[3].Attributes["value"].Value);

                exp.CheckInType_SU = double.Parse(node.ChildNodes[4].Attributes["value"].Value);

                exp.Ledger_ExtraHalfDay = double.Parse(node.ChildNodes[5].Attributes["value"].Value);

                exp.Ledger_ExtraFullDay = double.Parse(node.ChildNodes[6].Attributes["value"].Value);

                _formulas.Add(exp);
            }

            return _formulas;
        }
        
       
        public static string GetRootPath()
        {
            string root = System.Web.Hosting.HostingEnvironment.MapPath("~") != null
                ? System.Web.Hosting.HostingEnvironment.MapPath("~") + @"\bin\"
                : string.Empty;

            if (string.IsNullOrEmpty(root))
            {
                root = (System.Web.HttpContext.Current != null)
                              ? System.Web.HttpContext.Current.Request.MapPath("~") + @"\bin\"
                              : string.Empty;
            }

            return root;
        }

        public int Pressure
        {
            get
            {
                if (_pressure == default(int))
                {
                    lock (_locker)
                    {
                        if (_pressure == default(int))
                        {
                            _pressure = LoadPressure();

                        }
                    }
                }

                return _pressure;
            }
        }

        private int LoadPressure()
        {
            XmlDocument doc = new XmlDocument();

            string path = GetRootPath() + @"formula.xml";

            doc.Load(path);

            XmlNodeList nodes = doc.SelectNodes("/formula/pressure");

            foreach (XmlNode node in nodes)
            {
                int.TryParse(node.Attributes["value"].Value, out _pressure);
            }

            return _pressure;
        }
    }

    
    public class Formula
    {
        
        public string Name { get; set; }

        /// <summary>
        /// [标准/过夜/正常/全天] 房
        /// </summary>
        public double CheckInType_NS { get; set; }
        
        /// <summary>
        /// [白日/白天] 房
        /// </summary>
        public double CheckInType_DR { get; set; }
        
        /// <summary>
        /// [小时/时租]房
        /// </summary>
        public double CheckInType_HR { get; set; }

        /// <summary>
        /// [自用/招待房]
        /// </summary>
        public double CheckInType_SU { get; set; }

        /// <summary>
        /// 免费房
        /// </summary>
        public double CheckInType_FR { get; set; }

        /// <summary>
        /// 加收半天
        /// </summary>
        public double Ledger_ExtraHalfDay { get; set; }

        /// <summary>
        /// 加收全天
        /// </summary>
        public double Ledger_ExtraFullDay { get; set; }
    }
}
